package cn.edu.neusoft.amicooldemo.model;


import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.List;

import cn.edu.neusoft.amicooldemo.bean.CollectResult;
import cn.edu.neusoft.amicooldemo.iface.AmicoolListener;
import cn.edu.neusoft.amicooldemo.iface.CollectListListener;
import cn.edu.neusoft.amicooldemo.iface.CollectListener;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by neusoft on 2017/1/5.
 */

public class CallbackFocus<T> implements Callback<List<T>>{
    private AmicoolListener listener;
    private int page;

    public CallbackFocus(int page, AmicoolListener listener) {
        this.page = page;
        this.listener = listener;
    }


    @Override
    public void onResponse(Call<List<T>> call, Response<List<T>> response) {
        if (response.raw().code() == 200) {//200是服务器有合理响应
            if(response.isSuccessful()&&response.body()!=null){
                if(page==1)
                    listener.onResponse(response.body());
                else
                    listener.loadNextData(response.body());
            }
            else {
                listener.onFail("0");
            }

        } else {//失败响应
            onFailure(call, new RuntimeException("response error,detail = " + response.raw().toString()));
        }
    }

    @Override
    public void onFailure(Call<List<T>> call, Throwable t) {
        if(t instanceof SocketTimeoutException){
            //
        }else if(t instanceof ConnectException){
            //
        }else if(t instanceof RuntimeException){
            //
        }
        listener.onFail(t.getMessage());
    }
}
